package com.ymatou.authapp.testcase;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.authapp.parameter.PasswordRequestDtoBean;
import com.ymatou.authapp.service.PasswordRequestDtoCall;
import com.ymatou.operate.trading.api.parameter.OrderPayRequestBean;
import com.ymatou.operate.trading.api.service.OrderPayRequestCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.AuthCallService;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.business.service.UserCallService;
import com.ymttest.common.enums.PlatformEnum;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.constant.ECode;

/**
 * 移除认证 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_PasswordRequestDto {
	private static PasswordRequestDtoBean lab;
	private static PasswordRequestDtoCall lac;
	private static String UserName;
	private static String userId;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("修改密码");
		Map UserMap = UserCallService.registerByMobile();
		userId = UserMap.get("userid").toString();
		UserName = UserMap.get("username").toString();
		// 每个case开始前重置账号密码
		AuthCallService.modifyLoginPassword(userId, "123456a");
	}

	@Before
	public void caseUp() {
		lab = new PasswordRequestDtoBean();
		lac = new PasswordRequestDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(lac.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_PasswordRequestDto_001() {
		Logger.start(true,"修改登入密码成功");
		try {
			Map m = AuthCallService.authLogin(UserName, "123456a");
			Logger.comment("登入成功:" + m);
			String accessToken1 = m.get("accessToken").toString();
			lab.setAType("ByUserId");
			lab.setPasActionType("ModifyLoginPassword");
			lab.setUserId(userId);
			lab.setNewPassword("654321");
			lac.setData(lab);
			Logger.comment("调用修改密码接口");
			lac.callService();
			Thread.sleep(3000);
			Logger.comment("修改密码后登入用户:" + UserName);
			Thread.sleep(1000);
			Map m2 = AuthCallService.authLogin(UserName, "654321");
			Logger.comment("修改密码后登入成功:" + m2);
			// 如果登入失败（修改密码失败），accessToken2获取时会抛空指针异常
			String accessToken2 = m2.get("accessToken").toString();
			Logger.verifyNotEquals(accessToken1, accessToken2, "验证修改登入密码前后accessToken不一致");
			Thread.sleep(1000);
			Map sam2 = AuthCallService.signAuth(accessToken2);
			Logger.verifyEquals(true, sam2.get("success"), "验证第二次的Token有效");
			Thread.sleep(3000);
			Map sam1 = AuthCallService.signAuth(accessToken1);
			Logger.verifyEquals(false, sam1.get("success"), "验证第一次的Token失效");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	// 支付密码需要调用userservice，需要业务验证
	 @Test
	 @Category(P2.class)
	 @TestCase
	 @Ignore
	public void Tc_PasswordRequestDto_002() {
		Logger.start(true,"修改支付密码成功");
		try {
			String newTradingpwd = "123abc";
			Map m = AuthCallService.authLogin(UserName, "123456a");
			Logger.comment("登入成功:" + m);
			String accessToken1 = m.get("AccessToken").toString();
			lab.setAType("ByUserId");
			lab.setPasActionType("ModifyTradingPassword");
			lab.setUserId(userId);
			lab.setNewPassword(newTradingpwd);
			lac.setData(lab);
			Logger.comment("调用修改密码接口");
			lac.callService();
			Map m2 = AuthCallService.authLogin(UserName, "123456a");
			Logger.comment("修改支付后登入成功:" + m2);
			// 如果登入失败（修改密码失败），accessToken2获取时会抛空指针异常
			String accessToken2 = m2.get("AccessToken").toString();
			Logger.verifyEquals(accessToken1, accessToken2, "验证修改支付密码前后登入accessToken一致");

			// 支付验证充值密码是否有效
			OrderPayRequestBean orderpayrequestbean = new OrderPayRequestBean();
			OrderPayRequestCall orderpayrequestcall = new OrderPayRequestCall();
			Map map = new OrderCallService().appSaveOrder();
			int OrderId = (Integer) map.get("OrderId");
			int UserId = (Integer) map.get("UserId");
			int SellerId = (Integer) map.get("SellerId");
			int TradingId = (Integer) map.get("TradingId");

			// 支付前账户金额
			DefaultWapper database = new DefaultWapper();
			HashMap<String, Integer> m1 = new HashMap<String, Integer>();
			m1.put("UserId", UserId);
			BigDecimal price1 = (BigDecimal) database.selectAccountInfo(m1).get(0).get("fAvailAmount");
			BigDecimal price3 = (BigDecimal) map.get("TotalPrice");
			// 参数
			orderpayrequestbean.setCallBackUrl("www.baidu.com");
			orderpayrequestbean.setUserId(UserId);
			orderpayrequestbean.setUseAccountAmount(price1);
			orderpayrequestbean.setTradingId(TradingId);
			Logger.comment("使用新的支付密码进行支付");
			orderpayrequestbean.setTradingPwd(newTradingpwd);

			orderpayrequestbean.setUserIp("127.0.0.10");
			orderpayrequestbean.setBankId("");
			orderpayrequestbean.setPayType("10");
			orderpayrequestbean.setUsePlatform(PlatformEnum.Shangou_WAP);
			orderpayrequestbean.setParamExt("");
			orderpayrequestbean.setMemo("备注");

			// 发送请求
			orderpayrequestcall.setData(orderpayrequestbean);
			orderpayrequestcall.callService();
			Logger.verifyEquals(ECode.SUCCESS, orderpayrequestcall.getCode(), "验证Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PasswordRequestDto_003() {
		Logger.start(false,"未输入新密码，失败");
		try {
			Logger.comment("登入用户:" + UserName);
			Map m = AuthCallService.authLogin(UserName, "123456a");
			Logger.comment("登入成功:" + m);
			lab.setAType("ByUserId");
			lab.setPasActionType("ModifyLoginPassword");
			lab.setUserId(userId);
			lac.setData(lab);
			Logger.comment("调用修改密码接口");
			lac.callService();
			Logger.verifyEquals(false, lac.getSuccess(), "验证是否成功");
			Logger.verifyEquals(ECode.AUTH_NOPWD, lac.getString("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PasswordRequestDto_004() {
		Logger.start(false,"输入错误的ActionType");
		try {
			Logger.comment("登入用户:" + UserName);
			Map m = AuthCallService.authLogin(UserName, "123456a");
			Logger.comment("登入成功:" + m);
			lab.setAType("Other");
			lab.setNewPassword("654321");
			lab.setPasActionType("ModifyLoginPassword");
			lab.setUserId(userId);
			lac.setData(lab);
			Logger.comment("调用修改密码接口");
			lac.callService();
			Logger.verifyEquals(false, lac.getSuccess(), "验证是否成功");
			Logger.verifyEquals(ECode.AUTH_WRONG_ACTIONTYPE, lac.getString("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}